package coreservlets.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FirstGwtApp implements EntryPoint {

	public void onModuleLoad() {
		textBoxSetup();
		buttonSetup();
	}
	
	private void textBoxSetup() {
		TextBox txtName = new TextBox();
		txtName.setText("Enter your name here...");
		txtName.addClickHandler(new TxtNameClickHandler(txtName));
		RootPanel.get("textBoxNameHolder").add(txtName);
	}
	
	private void buttonSetup(){
		Button buttonGreet = new Button("Greet!");
		Label labelGreet = new Label();
		buttonGreet.addClickHandler(new ButtonGreetHandler(labelGreet));
		RootPanel.get("buttonGreetHolder").add(buttonGreet);
		RootPanel.get("labelGreetHolder").add(labelGreet);
	}
	
	private class ButtonGreetHandler implements ClickHandler {
		private Label label;
		private String name;
		
		public ButtonGreetHandler(Label label){
			this.label = label;
		}
		
		public void onClick(ClickEvent event){
			name = ((TextBox)RootPanel.get("textBoxNameHolder").getWidget(0)).getText();
			label.setText("Hello, " + name);
		}
	}
	
	private class TxtNameClickHandler implements ClickHandler {
		private TextBox textBox;
		
		public TxtNameClickHandler(TextBox textBox){
			this.textBox = textBox;
		}
		@Override
		public void onClick(ClickEvent event) {
			if (this.textBox.getText().equals("Enter your name here...")){
				this.textBox.setText("");
			}
		}
	}
}
